package com.vhuomai.system.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.system.constant.UserRole;
import com.vhuomai.system.entity.BaseSysLink;
import com.vhuomai.system.service.SystemLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harris
 */
@Api(tags = "系统链接")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class LinkController {
    
    @Autowired
    private SystemLinkService linkService;
    
    @ApiOperation(value = "链接列表")
    @GetMapping(value = "/system/links", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseSysLink>> links() {
        return ServiceResult.success(linkService.findAll());
    }
    
    @ApiOperation(value = "添加链接")
    @PostMapping(value = "/system/link", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseSysLink> addLink(@RequestBody BaseSysLink link) {
        return ServiceResult.success(linkService.add(link));
    }
    
    @ApiOperation(value = "修改链接")
    @PutMapping(value = "/system/link", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateLink(@RequestBody BaseSysLink link) {
        linkService.update(link);
        return ServiceResult.SUCCESS;
    }
    
    @ApiOperation(value = "删除链接")
    @DeleteMapping(value = "/system/link", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> removeLink(@PathVariable("id") int id) {
        linkService.remove(id);
        return ServiceResult.SUCCESS;
    }
}
